package com.kkshop.gwt.widget.wizard.impl.stackpanel;

import java.util.Iterator;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.FocusWidget;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.kkshop.gwt.widget.wizard.WizardPage;
import com.kkshop.gwt.widget.wizard.WizardWidget;

public class WizardPageWrapper {
    private WizardPage wizardPage;
    private WizardWidget wizardWidget;

    VerticalPanel panel;
    private Button nextButton;
    private Button prevButton;
    private Label errorMessage;

    private boolean isActive = false;
    private Widget wizardPageWidget;
    private Button finishButton;
    private Button cancelButton;

    public WizardPageWrapper(WizardWidget wizardWidget, WizardPage wizardPage) {
        this.wizardWidget = wizardWidget;
        this.wizardPage = wizardPage;
    }

    public void updateButtons() {
        if (nextButton != null) {
            nextButton.setEnabled(isActive && wizardPage.canFlipToNextPage());
        }
        if (finishButton != null) {
            finishButton.setEnabled(isActive && wizardPage.getWizard().canFinish());
        }
        if (prevButton != null) {
            prevButton.setEnabled(isActive);
        }
    }

    public void updateMessage() {
        String errorMsg = wizardPage.getErrorMessage();
        if (errorMsg != null) {
            errorMessage.setText(errorMsg);
            errorMessage.setVisible(true);
        } else {
            errorMessage.setVisible(false);
        }
    }

    public WizardPage getWizardPage() {
        return wizardPage;
    }

    public void setActive(boolean active) {
        isActive = active;
        setRecursiveActive(wizardPageWidget, active);
        if (active) {
            panel.setStyleName("sph-Wizard-StackPanel-active");
        } else {
            panel.setStyleName("sph-Wizard-StackPanel");
        }
        updateButtons();
    }

    protected void setRecursiveActive(Widget widget, boolean active) {
        if (widget instanceof FocusWidget) {
            ((FocusWidget) widget).setEnabled(active);
            if (!active) {
                widget.addStyleName("sph-Wizard-disabled");
            }
            else {
                widget.removeStyleName("sph-Wizard-disabled");
            }
        }
        if (widget instanceof HasWidgets) {
            for (Iterator i = ((HasWidgets) widget).iterator(); i.hasNext();) {
                Widget w = (Widget) i.next();
                setRecursiveActive(w, active);
            }
        }
    }

    public Widget createWidget() {
        if (panel != null) {
            return panel;
        }
        panel = new VerticalPanel();
        panel.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);

        panel.add(wizardPageWidget = wizardPage.createWidget());

        panel.add(errorMessage = new Label());
        errorMessage.setVisible(false);
        errorMessage.setStyleName("sph-Wizard-ErrorMessage");

        HorizontalPanel buttonPanel = new HorizontalPanel();

        if (wizardPage.getWizard().getPrevWizardPage(wizardPage) != null) {
            buttonPanel.add(prevButton = new Button("Back"));
            prevButton.addClickListener(new ClickListener() {
                public void onClick(Widget sender) {
                    wizardWidget.handlePrevClick();
                }
            });
        }
        if (wizardPage.getWizard().getNextWizardPage(wizardPage) != null) {
            buttonPanel.add(nextButton = new Button("Next"));
            nextButton.addClickListener(new ClickListener() {
                public void onClick(Widget sender) {
                    wizardWidget.handleNextClick();
                }
            });
        } else {
            buttonPanel.add(finishButton = new Button("Finish"));
            finishButton.addClickListener(new ClickListener() {
                public void onClick(Widget sender) {
                    wizardWidget.handleFinishClick();
                }
            });
        }
        buttonPanel.add(cancelButton = new Button("Cancel"));
        cancelButton.addClickListener(new ClickListener() {
            public void onClick(Widget sender) {
                wizardWidget.handleCancelClick();
            }
        });

        panel.add(buttonPanel);
        panel.setCellHorizontalAlignment(buttonPanel, VerticalPanel.ALIGN_RIGHT);

        setActive(false);
        updateButtons();
        updateMessage();

        return panel;
    }
}
